package com.txsj.scan.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.txsj.scan.R;
import com.txsj.scan.bean.WarehouseBean;

import java.util.ArrayList;
import java.util.List;

public class WareHouseAdapter extends RecyclerView.Adapter<WareHouseAdapter.WareHouseViewHolder> {

    private List<WarehouseBean> list;
    private Context context;

    public WareHouseAdapter(Context context) {
        this.context = context;
        this.list = new ArrayList<>();
    }

    public void setList(List<WarehouseBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public WareHouseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new WareHouseViewHolder(LayoutInflater.from(context).inflate(R.layout.item_house, parent, false));
    }

    @Override
    public void onBindViewHolder(WareHouseViewHolder holder, final int position) {
        holder.tv_house.setText(list.get(position).getWarehouse_name());
        holder.tv_house.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != listener)listener.onCLick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return (null == list || list.size() == 0) ? 0 : list.size();
    }

    public class WareHouseViewHolder extends RecyclerView.ViewHolder {

        TextView tv_house;

        public WareHouseViewHolder(View itemView) {
            super(itemView);
            tv_house = (TextView) itemView.findViewById(R.id.tv_house);
        }
    }

    private  OnItemClickListener listener;

    public void setListener( OnItemClickListener listener) {
        this.listener = listener;
    }

    public static interface OnItemClickListener {
        void onCLick(int position);
    }
}
